package microarray;

import java.io.*;
import java.util.Stack;

public class MicroArrayCrawler extends MicroArrayElement {
	private Stack<Platform> platforms;
	private String topLevelDirectory;
	private boolean debug = false;
	private boolean overwrite = false;

	public MicroArrayCrawler(String topLevelDirectory) throws IOException {
		super("index");
		this.topLevelDirectory = topLevelDirectory;
		platforms = new Stack<Platform>();
	}

	public boolean isDebug() {
		return debug;
	}

	public boolean overwriteWhenFileExists() {
		return overwrite;
	}

	public void addPlatform(Platform p) {
		platforms.push(p);
	}

	public void addPlatform(String s) throws IOException {
		platforms.push(new Platform(s, this));
	}

	@Override
	public String getDir() {
		return topLevelDirectory;
	}

	public void scrapeAndRecord() throws IOException {
		Platform p;

		while (!platforms.empty()) {
			p = platforms.pop();
			try {
				System.out.println(p.id);
				p.scrapeAndRecord();
			} catch (IOException e) {
				System.out.println("scrape/record platform " + p + " failed: "
						+ e);
			}
		}
		/*
		 * Series s;
		 * 
		 * while (!series_ids.empty()) { s = new Series(series_ids.pop(), this);
		 * try { System.out.println(s.id); s.scrapeAndRecord(); } catch
		 * (IOException e) { System.out.println("scrape/record series " + s +
		 * " failed: " + e); } }
		 */
	}

	public void build() throws IOException {
		for (Platform p : platforms) {
			p.build();
		}
	}

	// http://www.ncbi.nlm.nih.gov/geo/browse/?view=platform&search=mus%20musculus%20affymetrix&display=20&sort=acc
	public static void main(String[] args) throws IOException {

		String platformInput = "PLATFORMS.txt";
		String outputDir = "microarrays/";

		boolean ow = false; // overwrite
		boolean db = false; // debug
		for (int i = 0; i < args.length;) {
			if (args[i].equals("-o")) {
				ow = true;
			} else if (args[i].equals("-d")) {
				db = true;
			} else if (args[i].equals("-p")) {
				if (args.length > (i + 1)) {
					i++;
					platformInput = args[i];
				}
			} else if (args[i].equals("-f")) {
				if (args.length > (i + 1)) {
					i++;
					outputDir = args[i];
				}
			}
			i++;
		}

		MicroArrayCrawler m = new MicroArrayCrawler(outputDir);
		m.debug = db;
		m.overwrite = ow;

		File f = new File(platformInput);
		if (!f.exists()) {
			System.out.println("No platforms found in " + platformInput);
		} else {
			BufferedReader in = new BufferedReader(new FileReader(f));
			for (String line = in.readLine(); line != null; line = in
					.readLine()) {
				line = line.trim();
				if (line.startsWith("GPL"))
					m.addPlatform(line.trim());
			}
		}

		m.build();
		m.scrapeAndRecord();

		/*
		 * These appear to be acceptable.
		 * 
		 * m.addPlatform("GPL32"); m.addPlatform("GPL33");
		 * m.addPlatform("GPL34"); m.addPlatform("GPL75");
		 * m.addPlatform("GPL76"); m.addPlatform("GPL81");
		 * m.addPlatform("GPL82"); m.addPlatform("GPL83");
		 * m.addPlatform("GPL339"); m.addPlatform("GPL340");
		 * m.addPlatform("GPL1261"); m.addPlatform("GPL1857");
		 * m.addPlatform("GPL3576"); m.addPlatform("GPL5008");
		 * m.addPlatform("GPL5722"); m.addPlatform("GPL5759");
		 * m.addPlatform("GPL5766"); m.addPlatform("GPL6096");
		 * m.addPlatform("GPL6246"); m.addPlatform("GPL6456");
		 * m.addPlatform("GPL6526"); m.addPlatform("GPL7368");
		 * m.addPlatform("GPL7546"); m.addPlatform("GPL7635");
		 * m.addPlatform("GPL6886"); m.addPlatform("GPL8059");
		 * m.addPlatform("GPL8462"); m.addPlatform("GPL8492");
		 * m.addPlatform("GPL9523"); m.addPlatform("GPL9746");
		 * m.addPlatform("GPL10288"); m.addPlatform("GPL10369");
		 * m.addPlatform("GPL10773"); m.addPlatform("GPL11044");
		 */

		/*
		 * These platform tables are missing
		 * 
		 * m.addPlatform("GPL5811"); m.addPlatform("GPL6444");
		 * m.addPlatform("GPL6445"); m.addPlatform("GPL6446");
		 * m.addPlatform("GPL6447"); m.addPlatform("GPL6448");
		 * m.addPlatform("GPL6449"); m.addPlatform("GPL6450");
		 */
		/*
		 * These platform tables are absurdly big (100K+)
		 * 
		 * m.addPlatform("GPL6193"); m.addPlatform("GPL6613");
		 * m.addPlatform("GPL6614"); m.addPlatform("GPL6615");
		 * m.addPlatform("GPL6616"); m.addPlatform("GPL6617");
		 * m.addPlatform("GPL6618"); m.addPlatform("GPL6619");
		 * m.addPlatform("GPL6620"); m.addPlatform("GPL6621");
		 * m.addPlatform("GPL6622"); m.addPlatform("GPL6623");
		 * m.addPlatform("GPL6624"); m.addPlatform("GPL6625");
		 * m.addPlatform("GPL6626"); m.addPlatform("GPL6627");
		 * m.addPlatform("GPL6628");
		 */
		/*
		 * These don't have any samples.
		 * 
		 * m.addPlatform("GPL77"); m.addPlatform("GPL78");
		 * m.addPlatform("GPL79"); m.addPlatform("GPL6638");
		 * m.addPlatform("GPL8760"); m.addPlatform("GPL11098");
		 * m.addPlatform("GPL11180"); m.addPlatform("GPL11533");
		 * m.addPlatform("GPL13194"); m.addPlatform("GPL13195");
		 * m.addPlatform("GPL13196"); m.addPlatform("GPL13197");
		 * m.addPlatform("GPL13198"); m.addPlatform("GPL13199");
		 * m.addPlatform("GPL13200"); m.addPlatform("GPL13201");
		 * m.addPlatform("GPL13202"); m.addPlatform("GPL13203");
		 * m.addPlatform("GPL13204"); m.addPlatform("GPL13205");
		 * m.addPlatform("GPL13206"); m.addPlatform("GPL13207");
		 */
	}
}